package com.xunhang.controller;

import com.xunhang.service.Crawler2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/api/college-news")
public class Crawler2Controller {

    private final Crawler2Service crawler2Service;

    @Autowired
    public Crawler2Controller(Crawler2Service crawler2Service) {
        this.crawler2Service = crawler2Service;
    }

    @GetMapping("/single")
    public ResponseEntity<String> crawlSingle(
            @RequestParam(defaultValue = "https://uc.whu.edu.cn/xwzx/xyfc.htm") String url) {
        try {
            crawler2Service.crawlSingleCollegeNews(url);
            return ResponseEntity.ok("学院新闻抓取任务已启动");
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("抓取失败: " + e.getMessage());
        }
    }

    @GetMapping("/batch")
    public ResponseEntity<String> crawlBatch(
            @RequestParam(defaultValue = "5") int count,
            @RequestParam(defaultValue = "https://uc.whu.edu.cn/xwzx/xyfc.htm") String url) {
        try {
            crawler2Service.crawlBatchCollegeNews(url, count);
            return ResponseEntity.ok("学院新闻批量抓取任务已启动");
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("批量抓取失败: " + e.getMessage());
        }
    }
}